package pers.zhang.util;

import com.thoughtworks.xstream.XStream;
import org.apache.http.client.utils.DateUtils;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import pers.zhang.dto.TextMessage;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.io.InputStream;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Classname MessageUtil
 * @Description TODO
 * @Date 2019/1/18 15:02
 * @Created by zhangkai
 */
public class MessageUtil {
    public static  final  String MESSAGE_TEXT="text";
    public static  final  String MESSAGE_IMAGE="image";
    public static  final  String MESSAGE_VOICE="voice";
    public static  final  String MESSAGE_VIDEO="video";
    public static  final  String MESSAGE_LINK="link";
    public static  final  String MESSAGE_LOCATION="location";
    //关注
    public static  final  String MESSAGE_EVENT="event";
    //关注
    public static  final  String MESSAGE_SUBSCRIBE="subscribe";
    //取消关注
    public static  final  String MESSAGE_UNSUBSCRIBE="unsubscribe";
    public static  final  String MESSAGE_CLICK="CLICK";
    public static  final  String MESSAGE_VIEW="VIEW";


    /**
     * 获取消息xml转成map
     * @param request
     * @return
     * @throws IOException
     * @throws DocumentException
     */
    public static Map<String,String> xmlToMap(HttpServletRequest request) throws IOException, DocumentException {
        Map<String,String> map=new HashMap<String, String>();
        SAXReader reader=new SAXReader();
        InputStream ins=request.getInputStream();
        Document document=reader.read(ins);
        Element root=document.getRootElement();
        List<Element> list=root.elements();
        for (Element element:list)
        {
            map.put(element.getName(),element.getText());
        }
        ins.close();
        return map;
    }

    /**
     * 将消息对象转为xml
     * @param textMessage
     * @return
     * @throws IOException
     * @throws DocumentException
     */
    public static String textToXml(TextMessage textMessage) {
    XStream xStream= new XStream();
    xStream.alias("xml",textMessage.getClass());
    return xStream.toXML(textMessage);
    }

    /**
     * 主菜单
     * @return
     */
    public  static String menuText(){
        StringBuffer stringBuffer=new StringBuffer();
        stringBuffer.append("欢迎您的关注，请按照菜单提示进行操作：\n\n");
        stringBuffer.append("1.小诗\n");
        stringBuffer.append("2.推送歌曲\n");
        stringBuffer.append("3. 学说话\n");
        stringBuffer.append("回复：调出此菜单\n");
        return stringBuffer.toString();
    }
    /**
     * 主菜单 1
     * @return
     */
    public  static String firstText(){
        StringBuffer stringBuffer=new StringBuffer();
        stringBuffer.append("欢迎您的关注,First\n\n");
        return stringBuffer.toString();
    }

    /**
     * 编辑发送消息
     * @param toUserName
     * @param fromUserName
     * @param content
     * @return
     */
    public  static String initText(String toUserName,String fromUserName,String content){
        TextMessage textMessage=new TextMessage();
        textMessage.setFromUserName(toUserName);
        textMessage.setToUserName(fromUserName);
        textMessage.setMsgType(MessageUtil.MESSAGE_TEXT);
        textMessage.setCreateTime((int) new Date().getTime());
        textMessage.setContent(content);
        return textToXml(textMessage);
    }
}
